﻿using System.Windows;
using System.Windows.Input;
using Caliburn.Micro;
using HandyControl.Controls;
using ThunderboltInterfaceSharp.ViewModels.UserControls;
using ThunderboltInterfaceSharp.ViewModels.UserControls.CloudDisk;
using ThunderboltInterfaceSharp.ViewModels.UserControls.Download;
using ThunderboltInterfaceSharp.ViewModels.UserControls.More;
using ThunderboltInterfaceSharp.Views;
using Window = HandyControl.Controls.Window;

namespace ThunderboltInterfaceSharp.ViewModels;

public class ThunderMainViewModel : Screen
{
	private Point _pressedPosition;
	private bool _isDragMoved = false;
	private ThunderMainView _mainView;
	public TopLevelSidebarViewModel TopLevelSidebar { get; set; }
	public TopFunctionBarViewModel TopFunctionBar { get; set; }

	private ConfirmExitDialogViewModel ConfirmExitDialog { get; set; }

	public LoginViewModel LoginWindow { get; set; }
	public Screen ViewModelScreen { get; set; }
	public MoreViewModel More { get; set; }
	public DownloadViewModel Download { get; set; }
	public CloudDiskViewModel CloudDisk { get; set; }
	public ThunderMainViewModel()
	{
		TopLevelSidebar = new TopLevelSidebarViewModel();
		TopLevelSidebar.SwitchFormEvent += SwitchForm;
		TopFunctionBar = new TopFunctionBarViewModel();
		TopFunctionBar.WindowChangeEvent += ExecuteWindowCommand;
		TopFunctionBar.ShowLoginWindowEvent += ShowLoginWindow;
		Download = new DownloadViewModel();
		CloudDisk = new CloudDiskViewModel();
		More = new MoreViewModel();
		ViewModelScreen = Download;
		ConfirmExitDialog = new ConfirmExitDialogViewModel();
		LoginWindow = new LoginViewModel();
	}


	//不要用private方法
	public void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
	{
		_pressedPosition = e.GetPosition(sender as ThunderMainView);
	}
	public void MouseMove(object sender, MouseEventArgs e)
	{
		//前端传入参数view而不是this
		var view = sender as ThunderMainView;
		if (Mouse.LeftButton == MouseButtonState.Pressed && _pressedPosition != e.GetPosition(view))
		{
			_isDragMoved = true;
			view?.DragMove();
		}
	}
	public void MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
	{
		if (_isDragMoved)
		{
			_isDragMoved = false;
			e.Handled = true;
		}
	}

	protected override void OnViewLoaded(object view)
	{
		_mainView = view as ThunderMainView;
		_mainView.MaxHeight = SystemParameters.PrimaryScreenHeight;//防止最大化时系统任务栏被遮盖
	}


	public void ExecuteWindowCommand(WindowChange order)
	{
		switch (order)
		{
			case WindowChange.Minimize: _mainView.WindowState = WindowState.Minimized;break;
			case WindowChange.Maximize: _mainView.WindowState = WindowState.Maximized;break;
			case WindowChange.Moderate: _mainView.WindowState = WindowState.Maximized;break;
			case WindowChange.Close:
				//todo:退出窗体showDialog,不要ShowWindowAsync
				var windowManager = IoC.Get<IWindowManager>();
				windowManager.ShowWindowAsync(ConfirmExitDialog);
				break;	
			default:  throw new System.NotImplementedException();
		}
	}

	public void SwitchForm(FormTitle title)
	{
		ViewModelScreen = title switch
			{
				FormTitle.Download => Download,
				FormTitle.CloudDisk => CloudDisk,
				//游戏和会员看着像web界面
				FormTitle.More => More,

				_ => Download
			};
	}

	public void ShowLoginWindow()
	{
		var windowManager = IoC.Get<IWindowManager>();
		windowManager.ShowWindowAsync(LoginWindow);
	}
}